/*
 * TablasLadoDerecho.java
 *
 * Created on 20 de julio de 2003, 12:19 AM
 */

package Tablas;

import javax.swing.table.*;

/**
 *
 * @author  David Santiago
 */
public class TablasLadoDerecho extends TablaGeneral {
    private TablasLadoDerechoModel modelo;
    
    /** Creates new TablasLadoDerecho */
    public TablasLadoDerecho(Object[][] data, String[] nombresCol) {
        setFilas(data.length);
        setContador(data.length);
        setColumnas(3);
        modelo = new TablasLadoDerechoModel(data, nombresCol);
        getTabla().setModel(modelo);
        
        scrollPane.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                jScrollPaneComponentResized(evt);
            }
        });
        setAnchoCol();
    }
    private void jScrollPaneComponentResized(java.awt.event.ComponentEvent evt) {
        if (getWidth() < 270 )
            getTabla().setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        else
            getTabla().setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
    }
    
    private void setAnchoCol() {
        TableColumn column = null;
        for (int i = 0; i < getTabla().getColumnCount(); i++) {
            column = getTabla().getColumnModel().getColumn(i);
            column.setMinWidth(90);
        }
    }
    public void insertarFila(String s) {
        setFilas(getFilas() + 1);
        modelo.insertarFila(s);
    }
    public void eliminarFilas(int minIndex, int maxIndex) {
        if (minIndex != -1 && maxIndex != -1) {
            setFilas(getFilas() - (getMaxIndex() - getMinIndex() + 1));
            modelo.eliminarFila(getMinIndex(), getMaxIndex());
        }
    }
    
    public Object[][] getData() {
        return modelo.data;
    }
    
    
    public class TablasLadoDerechoModel extends AbstractTableModel {
        protected int NUM_COLUMNS;
        protected int NUM_ROWS;
        
        public String[] nombres;
        public Object[][] data;
        
        public TablasLadoDerechoModel(Object[][] d, String[] n) {
            NUM_ROWS = getFilas();
            NUM_COLUMNS = getColumnas();
            nombres = n;
            data = d;
        }
        
        public String getColumnName(int col) {
            return nombres[col];
        }
        public int getRowCount() {
            return NUM_ROWS;
        }
        public int getColumnCount() {
            return NUM_COLUMNS;
        }
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
        public boolean isCellEditable(int row, int col) {
            if (col == 0)
                return false;
            else
                return true;
        }
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
        
        public void insertarFila(String s) {
            Object[][] temp = new Object[NUM_ROWS][NUM_COLUMNS];
            temp = data;
            NUM_ROWS = NUM_ROWS + 1;
            data = new Object[NUM_ROWS][NUM_COLUMNS];
            for (int i = 0; i < NUM_ROWS -1; i++)
                for (int j = 0; j < NUM_COLUMNS; j++)
                    data[i][j] = temp[i][j];
            data[NUM_ROWS-1][0] = s;
            this.fireTableRowsInserted(NUM_ROWS -1 , NUM_ROWS);
        }
        
        public void eliminarFila(int fila1, int fila2) {
            int filasAeliminar = fila2 - fila1 + 1;
            Object[][] temp = new Object[NUM_ROWS - filasAeliminar][NUM_COLUMNS];
            for (int i = 0; i < fila1; i++)
                for (int j = 0; j < NUM_COLUMNS; j++)
                    temp[i][j] = data[i][j];
            for (int i = fila2 + 1; i < NUM_ROWS; i++)
                for (int j = 0; j < NUM_COLUMNS; j++)
                    temp[i-filasAeliminar][j] = data[i][j];
            
            NUM_ROWS = NUM_ROWS - filasAeliminar;
            data = new Object[NUM_ROWS][NUM_COLUMNS];
            for (int i = 0; i < NUM_ROWS; i++)
                for (int j = 0; j < NUM_COLUMNS; j++)
                    data[i][j] = temp[i][j];
            this.fireTableRowsDeleted(fila1, fila2);
        }
    }
}